package com.shuimin.as.service.sys;

import com.shuimin.as.App;
import pond.common.FILE;
import pond.common.S;
import pond.db.AbstractRecord;
import pond.db.DB;
import pond.db.Record;
import pond.db.connpool.ConnectionPool;
import pond.security.rbac.RBAC;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by ed on 12/24/15.
 */
public class SysRoles {

  //modules
  //TODO 修改成注册表性质，可被不同模块分类别注册，但是这样的话又没法使用java的反射了，捉急啊

  public static final String MOD_INDEX = "index";

  public static final String MOD_ADMIN = "admin";

  public static final String MOD_AFTER_SALE = "after_sale";


  public static enum Roles {

    //HR
    HR_USER_MGMT("HR_USER_MGMT", "用户-管理(添加修改用户)"),
    HR_USER_DELETE("HR_USER_DELETE", "用户-删除(删除用户--系统目前默认不可删除)"),
    HR_USER_PRIVILEGE("HR_USER_PRIVILEGE", "用户-权限分配"),
    HR_USER_SHOP("HR_USER_SHOP", "用户-门店分配"),
    HR_SALESMAN_MARK("HR_SALESMAN_MARK", "销售顾问-标记"),
    HR_EVALUATOR_MARK("HR_EVALUATOR_MARK", "评估师-标记"),

    //SYS
    RANK_MGMT("RANK_MGMT", "岗位-管理"),
    SHOP_MGMT("SHOP_MGMT", "门店-管理"),

    SUPER_ADMIN("SUPER_ADMIN", "超级管理员"),

    MANAGE_ACROSS_SHOPS("MANAGE_ACROSS_SHOPS", "跨门店管理(增、删、改）"),
    QUERY_ACROSS_SHOPS("QUERY_ACROSS_SHOPS", "跨门店查询"),
    QUERY_ACROSS_SHOPS_FOR_BRAND("QUERY_ACROSS_SHOPS_FOR_BRAND", "跨门店查询--按品牌")
    ;

    final Record value;

    final public String id;
    final public String name;

    Roles(String id, String name) {
      value = SysRoles.role(id, name);
      this.id = id;
      this.name = name;
    }

    public Record val() {return value;}

    @Override
    public String toString() {
      return id + " " + name;
    }
  }



  public static final String MENU_USER = "menu-user";

  //角色
  public static final String MENU_ROLE = "menu-role";

  //分配岗位
  public static final String MENU_PRIVILEGES = "MENU_PRIVILEGES";

  //岗位
  public static final String MENU_RANK = "MENU_RANK";

  public static final String MENU_SHOP = "menu-shop";
  public static final String MENU_USER_SHOP = "MENU_USER_SHOP";
  public static final String MENU_HOME = "menu-home";

  //******************上传文件**********************************
  public static final String MENU_UPLOAD = "MENU_UPLOAD";


  static Record role(String rid, String name) {
    return new AbstractRecord() {}
        .set("id", rid).set("rolename", "R-" + name);
  }

  static Record menu(String rid, String displayName, String module, String href) {
    return new AbstractRecord() {}
        .set("id", rid).set("rolename", displayName).set("href", href).set("module", module);
  }

  public static List<Record> all = new LinkedList<>();
  public static List<Record> menus = new LinkedList<>();
  public static List<Record> roles = new LinkedList<>();

  public static RBAC init() {
    S._for(Roles.values()).each(r -> {
      roles.add(r.val());
    });

    //lock
    roles = Collections.unmodifiableList(roles);


    menus.add(menu(MENU_HOME, "首页", MOD_INDEX, "/"));

    //========================系统======e===============================
    menus.add(menu(MENU_USER, "用户", MOD_ADMIN, "/users"));
    menus.add(menu(MENU_ROLE, "分配用户权限", MOD_ADMIN, "/privileges"));
    menus.add(menu(MENU_RANK, "岗位", MOD_ADMIN, "/ranks"));
    menus.add(menu(MENU_SHOP, "门店", MOD_ADMIN, "/shops"));
    menus.add(menu(MENU_USER_SHOP, "分配用户门店", MOD_ADMIN, "/shop_user"));
    menus.add(menu(MENU_UPLOAD, "上传数据", MOD_ADMIN, "/import"));
    menus.add(menu("MENU_SHOP_SORTER", "门店排序", MOD_ADMIN, "/shop_sorter"));
    menus.add(menu("MENU_AFTER_SALE_ORDER", "维修订单列表", MOD_AFTER_SALE, "/repair/order"));
    menus.add(menu("MENU_AFTER_SALE_SHOP_MAPPER", "门店导入字段映射", MOD_AFTER_SALE, "/repair/shop_mapper"));


    //lock
    menus = Collections.unmodifiableList(menus);

    //roles.add(role(MENU_VEHICLE, "车型管理"));
    //merge & lock
    List<Record> l = new LinkedList<>();
    l.addAll(menus);
    l.addAll(roles);
    all = Collections.unmodifiableList(l);

    return new RBAC(App.DB).init()
        .label_user_id("id")
            //set root id to override all
        //
        .root("886931671a654e538eae16738e5b33b7")
        .label_username("name")
        .label_role_id("id")
        .label_rolename("rolename")
        .sync(UserService.briefForRole(), all);
  }

  public static void main(String[] args) {
    new RBAC(new DB(ConnectionPool.c3p0(FILE.loadProperties("cp.conf")))).reset()
            .label_user_id("id")
            .root("886931671a654e538eae16738e5b33b7");
  }

}
